package com.example.controller;

import com.alibaba.fastjson2.JSONObject;
import com.example.common.Result;
import com.example.model.logBo.DataDetail;
import com.example.model.logBo.TenantDto;
import com.example.model.logBo.TenantVo;
import com.example.service.TenantService;
import com.example.utils.PropertyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@Slf4j
@RestController
@CrossOrigin//支持跨域
@RequestMapping("/blockchain/api")
public class TenantController {

    @Autowired
    private TenantService tenantService;

    /**
     * 上传操作日志
     * @param tenantDto 日志信息
     * @return
     * @throws Exception
     */
    @PostMapping("/data/upload")
    public Result upload(@RequestBody TenantDto tenantDto) throws Exception {
        log.info("收到日志数据:{}", JSONObject.toJSONString(tenantDto));
        //上传日志
        tenantService.upload(tenantDto);
        return Result.success("上传成功");
    }

    /**
     * 根据租户ID查询历史日志
     * @return
     * @throws Exception
     */
    @GetMapping("/dataList")
    public Result getByTenantId() throws Exception {
        String realId = PropertyUtil.getRealName();
        System.out.println("realm:"+realId);
        //根据租户id查询产品信息
        List<TenantVo> tenantVos = tenantService.getByTenantId(realId);
        Collections.reverse(tenantVos);
        return Result.success(tenantVos);

    }

    /**
     * 根据系统ID查询日志详细信息
     * @param sysId 系统ID
     * @return
     * @throws Exception
     */
    @GetMapping("/logList")
    public Result getDetailBySysId(@RequestParam(value = "sysId") String sysId) throws Exception {
        //根据系统id查询操作详情
        List<DataDetail> detailList = tenantService.getDetailBySysId(sysId);
        Collections.reverse(detailList);
        return Result.success(detailList);
    }

}
